using System.Threading;
using ByteDance.Union;
using UnityEngine;

/**
 * 自渲染banner代码示例
 * 注：仅支持穿山甲代码位，不支持融合
 */
public class ExampleBannerAd
{
    public static void LoadNativeBannerAd()
    {
        AdvCtrlManager.Instance().OnDisposeNativeAd();

        int width = UnityEngine.Screen.width;
        int height = width / 600 * 257;
Debug.LogError("banner 加载的宽度是："+height);
        var adSlot = new AdSlot.Builder()
            .SetCodeId(AdvCtrlManager.CSJ_NATIVE_BANNER_ID) // 必传
            .SetImageAcceptedSize(width, height) // 单位px
            .SetNativeAdType(AdSlotType.Banner) // 仅支持banner
            .Build();
        // LoadNativeAd接口仅支持自渲染Banner
        SDK.CreateAdNative().LoadNativeAd(adSlot, new NativeBannerAdListener());
    }

    public static void ShowNativeBannerAd()
    {
        var nativead = AdvCtrlManager.Instance().OnGetNativeAd();
        if (nativead == null)
        {
            Debug.LogError("CSJM_Unity "+"请先加载广告");
            return;
        }
        
        nativead.SetNativeAdInteractionListener(new NativeBannerAdInteractionListener());
        nativead.SetNativeAdDislikeListener(new NativeBannerAdDislikeCallback());
        nativead.SetDownloadListener(new AppDownloadListener());
        nativead.SetAdInteractionListener(new TTAdInteractionListener());
        nativead.ShowNativeAd(AdSlotType.Banner, 0, 500); // ShowNativeAd仅支持自渲染Banner
    }

    // 广告加载监听器
    public sealed class NativeBannerAdListener : INativeAdListener
    {
        public NativeBannerAdListener()
        {
        }

        public void OnError(int code, string message)
        {
            Debug.LogError("CSJM_Unity "+"OnNativeBannerAdError: " + message);
        }

        public void OnNativeAdLoad(NativeAd[] ads)
        {
            if (ads == null || ads.Length <= 0)
            {
                Debug.Log("CSJM_Unity "+$"OnNativeBannerAdLoad ads array is null on main thread: {Thread.CurrentThread.ManagedThreadId}");

                return;
            }
            AdvCtrlManager.Instance().OnGetNativeAd(ads[0]);

            Debug.Log("CSJM_Unity "+$"OnNativeAdLoad on main thread: {Thread.CurrentThread.ManagedThreadId}");
            //加载成功 开始显示
            ShowNativeBannerAd();
        }
    }

    public sealed class NativeBannerAdInteractionListener : IInteractionAdInteractionListener
    {
        public NativeBannerAdInteractionListener()
        {
        }

        public void OnAdCreativeClick()
        {
            Debug.Log("CSJM_Unity " + $"NativeAd creative click on main thread: {Thread.CurrentThread.ManagedThreadId}");
        }

        public void OnAdShow()
        {
            Debug.Log("CSJM_Unity " + $"NativeAd show on main thread: {Thread.CurrentThread.ManagedThreadId}");
        }

        public void OnAdClicked()
        {
            Debug.Log("CSJM_Unity " + $"NativeAd click  on main thread: {Thread.CurrentThread.ManagedThreadId}");
            AdvCtrlManager.Instance().OnGetNativeAd().UploadDislikeEvent("csjm_unity nativeBanner dislike test");
        }

        public void OnAdDismiss()
        {
            Debug.Log("CSJM_Unity " + $"NativeAd close  on main thread: {Thread.CurrentThread.ManagedThreadId}");

            //释放广告资源
            AdvCtrlManager.Instance().OnDisposeNativeAd();
        }

        public void onAdRemoved()
        {
            Debug.Log("CSJM_Unity " + $"NativeAd onAdRemoved  on main thread: {Thread.CurrentThread.ManagedThreadId}");
        }
    }

    public class NativeBannerAdDislikeCallback : IDislikeInteractionListener
    {
        public NativeBannerAdDislikeCallback()
        {
        }

        public void OnCancel()
        {
            Debug.LogError("CSJM_Unity " + "native banner ad dislike OnCancel");
        }

        public void OnShow()
        {
            Debug.LogError("CSJM_Unity " + "native banner ad dislike OnShow");
        }

        public void OnSelected(int var1, string var2, bool enforce)
        {
            Debug.LogError("CSJM_Unity " + "native banner ad dislike OnSelected:" + var2);
        }
    }
}